﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Zyg.SSO.Core.Business;

namespace Zyg.SSO.Core.Services
{
    public class DALService
    {
        #region Constructor

        static DALService()
        {
            LoadProviders();
        }

        #endregion

        #region Provider model

        private static Providers.DALProviders.DALProvider provider;
        //private static Providers.DALProviders.SSOProviderCollection providers;
        private static object syncLock = new object();

        /// <summary>
        ///  Load the providers from the configuration file.
        /// </summary>
        private static void LoadProviders()
        {
            if (provider == null)
            {
                lock (syncLock)
                {
                    if (provider == null)
                    {
                        string name = Zyg.SSO.Configuration.ConfigurationManager.Providers.DALProviders.Default.Name;
                        string path = Zyg.SSO.Configuration.ConfigurationManager.Providers.DALProviders.Default.Path;
                        string className = Zyg.SSO.Configuration.ConfigurationManager.Providers.DALProviders.Default.ClassName;

                        provider = System.Reflection.Assembly.Load(path).CreateInstance(className) as Providers.DALProviders.DALProvider;
                    }
                }
            }
        }

        #endregion

        #region Account

        /// <summary>
        /// Returns a Account based on the specified id.
        /// </summary>
        public static Business.Account SelectAccount(Guid id)
        {
            return provider.SelectAccount(id);
        }

        public static Business.Account SelectAccount(string email)
        {
            return provider.SelectAccount(email);
        }

        public static void InsertAccount(Business.Account account)
        {
            provider.InsertAccount(account);
        }


        public static void DeleteAccount(Business.Account account)
        {
            provider.DeleteAccount(account);
        }

        public static void UpdateAccount(Business.Account account)
        {
            provider.UpdateAccount(account);
        }

        public static IList<Business.Account> FillAccounts()
        {
            return provider.FillAccounts();
        }

        public static IList<Business.Account> FillAccounts(Business.Role role)
        {
            return provider.FillAccounts(role);
        }

        public static IList<Business.Account> FillAccounts(string email, string name, Business.AccountState state, bool? isAdmin, Guid? roleId, DateTime? startTime, DateTime? endTime, int? startIndex, int? endIndex)
        {
            return provider.FillAccounts(email, name, state, isAdmin, roleId, startTime, endTime, startIndex, endIndex);
        }

        public static Boolean ExistsAccount(string email)
        {
            return provider.ExistsAccount(email);
        }
        #endregion

        #region Log

        public static void InsertLog(Business.Log log)
        {
            provider.InsertLog(log);
        }

        public static Business.Log SelectLog(int id)
        {
            return provider.SelectLog(id);
        }

        public static void UpdateLog(Business.Log log)
        {
            provider.UpdateLog(log);
        }

        public static void DeleteLog(Business.Log log)
        {
            provider.DeleteLog(log);
        }

        public static IList<Business.Log> FillLogs()
        {
            return provider.FillLogs();
        }


        #endregion

        #region Role

        /// <summary>
        /// Returns a Role based on the specified id.
        /// </summary>
        public static Business.Role SelectRole(Guid id)
        {
            return provider.SelectRole(id);
        }

        public static void InsertRole(Business.Role role)
        {
            provider.InsertRole(role);
        }

        public static void DeleteRole(Business.Role role)
        {
            provider.DeleteRole(role);
        }

        public static void UpdateRole(Business.Role role)
        {
            provider.UpdateRole(role);
        }

        public static Boolean ExistsRole(string name)
        {
            return provider.ExistsRole(name);
        }

        public static IList<Business.Role> FillRoles()
        {
            return provider.FillRoles();
        }

        #endregion

        #region App

        public static Business.App SelectApp(Guid id)
        {
            return provider.SelectApp(id);
        }

        public static void InsertApp(Business.App app)
        {
            provider.InsertApp(app);
        }

        public static void DeleteApp(Business.App app)
        {
            provider.DeleteApp(app);
        }

        public static void UpdateApp(Business.App app)
        {
            provider.UpdateApp(app);
        }

        public static IList<Business.App> FillApps()
        {
            return provider.FillApps();
        }

        #endregion

        #region AccountApp

        public static Business.AccountApp SelectAccountApp(Guid id)
        {
            return provider.SelectAccountApp(id);
        }

        public static Business.AccountApp SelectAccountApp(Guid accountId, Guid appId)
        {
            return provider.SelectAccountApp(accountId, appId);
        }

        public static void InsertAccountApp(Business.AccountApp accountApp)
        {
            provider.InsertAccountApp(accountApp);
        }

        public static void DeleteAccountApp(Business.AccountApp accountApp)
        {
            provider.DeleteAccountApp(accountApp);
        }

        public static void DeleteAccountApps(Business.Account account)
        {
            provider.DeleteAccountApps(account);
        }

        public static void DeleteAccountApps(Business.App app)
        {
            provider.DeleteAccountApps(app);
        }

        public static void UpdateAccountApp(Business.AccountApp accountApp)
        {
            provider.UpdateAccountApp(accountApp);
        }

        public static IList<AccountApp> FillAccountApps(Business.Account account)
        {
            return DALService.FillAccountApps(account, null);
        }

        public static IList<AccountApp> FillAccountApps(Business.App app)
        {
            return DALService.FillAccountApps(null, app);
        }

        public static IList<AccountApp> FillAccountApps(Business.Account account, Business.App app)
        {
            return provider.FillAccountApps(account, app);
        }

        public static void DeleteUpdateAccountApps(Business.Account account, Business.App app, IList<Business.AccountApp> newRelations)
        {
            provider.DeleteUpdateAccountApps(account, app, newRelations);
        }

        #endregion

        #region AccountAppUser

        public static Business.AccountAppUser SelectAccoutAppUser(Guid id)
        {
            return provider.SelectAccountAppUser(id);
        }

        public static void InsertAccoutAppUser(Business.AccountAppUser aau)
        {
            provider.InsertAccountAppUser(aau);
        }

        public static void DeleteAccoutAppUser(Business.AccountAppUser aau)
        {
            provider.DeleteAccountAppUser(aau);
        }

        public static void DeleteAccountAppUsers(Business.AccountApp accountApp)
        {
            provider.DeleteAccountAppUsers(accountApp);
        }

        public static void UpdateAccoutAppUser(Business.AccountAppUser aau)
        {
            provider.UpdateAccountAppUser(aau);
        }

        public static IList<Business.AccountAppUser> FillAccountAppUsers(Business.AccountApp accountApp)
        {
            return provider.FillAccountAppUsers(accountApp);
        }

        #endregion

        #region RolePermission

        public static void InsertRolePermission(Business.RolePermission roleper)
        {
            provider.InsertRolePermission(roleper);
        }
        public static void UpdateRolePermission(Business.Role role, List<Business.RolePermission> rolepermissions)
        {
            provider.UpdateRolePermission(role, rolepermissions);
        }
        public static List<Business.Permission> SelectPermissionByRoleID(Guid roleid)
        {
            return provider.SelectPermissionByRoleID(roleid);
        }

        #endregion

        #region SysSetting

        public static Zyg.SSO.Core.Business.SysSetting SelectCurrentSysSetting()
        {
            return provider.SelectCurrentSetting();
        }

        public static void InsertSysSetting(Business.SysSetting setting)
        {
            provider.InsertSysSetting(setting);
        }

        #endregion

    }
}
